package com.jewelry.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jewelry.dto.CategoryDTO;
import com.jewelry.entity.Category;
import com.jewelry.entity.Product;
import com.jewelry.service.CategoryService;
import com.jewelry.mapper.CategoryMapper;
import com.jewelry.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LZL
 * @description 针对表【t_category】的数据库操作Service实现
 * @createDate 2024-03-21 20:53:04
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductService productService;


    @Override
    public List<CategoryDTO> getCategories() {
        return categoryMapper.selectCategories();
    }

    @Override
    public boolean addOrUpdateCategory(Category category) {
        return this.saveOrUpdate(category);
    }

    @Override
    public boolean deleteCategory(Integer id) {
        List<Category> categories = this.lambdaQuery().eq(Category::getParentId, id).list();
        if (ObjectUtil.isEmpty(categories)) {
            if (ObjectUtil.isEmpty(productService.lambdaQuery().eq(Product::getCategoryId, id).list())) {
                return this.removeById(id);
            }
            return false;
        } else {
            return false;
        }
    }
}




